<?php

namespace app\api\controller\kuyou\applet;

use think\Db;

class Datainfo extends Common
{
    public function stockwarning()
    {
        if ($this->request->isPost()) {
            $where = [];
            $goods_code = $this->request->post('goods_code');
            if(!empty($goods_code)){
                $where['goods_code'] = ['like','%'.$goods_code.'%'];
            }
            $purchase_time = $this->request->post('purchase_time');
            if(!empty($purchase_time)){
                $str = explode('~',$purchase_time);
                if(isset($str[0]) && isset($str[1])){
                    $where['purchase_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                }
            }
            $goods_name = $this->request->post('goods_name');
            if(!empty($goods_name)){
                $where['goods_name'] = ["like", '%'.$goods_name.'%'];
            }
            $type_id = $this->request->post('type_id');
            if(!empty($type_id)){
                $where['goods_type_id'] = ["in", $type_id];
            }
            $map = [];
            $stores_id = $this->request->post('stores_id');
            if(!empty($stores_id)){
                $map['stores_id'] = ["in", $stores_id];
            }
            $where['stock_warning'] = '1';

            $list = Db::name('kuyou_goods')
                ->where($where)
                ->field('id,unit_id,goods_type_id,manager_warehouse_id,goods_code,goods_name,goods_barcode,goods_image,unit_id,min_stock,max_stock,more_unit,inventory,branch')
                ->select();

            $datas = [];
            foreach ($list as $list_k=>$list_v){
                $unit = Db::name('kuyou_unit')->where(['id'=>$list_v['unit_id']])->field('id,name')->find();
                $list[$list_k]['unit'] = isset($unit['name'])?$unit['name']:'';
                if($unit){
                    $unit_name = $unit['name'];
                    $rowadvance_rate = '1';
                    if($list_v['more_unit']==1){
                        $more_unit = Db::name('kuyou_unit_conversion')->alias('uc')
                            ->join('kuyou_unit u','u.id=uc.vice_unit')
                            ->where(['uc.main_unit'=>$unit['id']])
                            ->order('uc.advance_rate','ASC')
                            ->field('uc.vice_unit,u.name,u.id,uc.advance_rate')
                            ->select();
                        foreach ($more_unit as $unit_k=>$unit_v){
                            $unit_name .= ','.$unit_v['name'];
                            $rowadvance_rate .= ':'.$unit_v['advance_rate'];
                        }
                        $list[$list_k]['unitconversion'] = $unit_name.'('.$rowadvance_rate.')';
                    }else{
                        $list[$list_k]['unitconversion'] = '';
                    }
                }
                if(empty($unit)){
                    $list[$list_k]['unit'] = ['name'=>''];
                }
                $goodstype = Db::name('kuyou_goods_type')->where(['id'=>$list_v['goods_type_id']])->field('supplier_type_name')->find();
                $list[$list_k]['goodstype'] = isset($goodstype['supplier_type_name'])?$goodstype['supplier_type_name']:'';
                $stores = Db::name('kuyou_stores')->where(['id'=>$list_v['manager_warehouse_id']])->field('name')->find();
                $list[$list_k]['stores'] = isset($stores['name'])?$stores['name']:'';
                $list[$list_k]['stock'] = $list_v['inventory'];
                if($list_v['branch']==1){
                    $lists = Db::name('kuyou_goods_stock_warning')->alias('sw')
                        ->join('kuyou_stores ks','ks.id=sw.stores_id')
                        ->where($map)
                        ->where(['sw.goods_code'=>$list_v['goods_code']])
                        ->field('sw.stores_id,sw.min_stock,sw.max_stock,ks.name')->select();
                    foreach ($lists as $lists_k=>$lists_v){
                        $stock = Db::name('kuyou_goods_stock')->where(['goods_code'=>$list_v['goods_code'],'stores_id'=>$lists_v['stores_id']])->value('stock');
                        $datas[] = [
                            'id' => $list[$list_k]['id'],
                            'goods_type_id' => $list[$list_k]['goods_type_id'],
                            'goodstype' => $list[$list_k]['goodstype'],
                            'goods_code' => $list_v['goods_code'],
                            'goods_name' => $list_v['goods_name'],
                            'goods_barcode' => $list_v['goods_barcode'],
                            'goods_image' => $list_v['goods_image'],
                            'more_unit' => $list[$list_k]['more_unit'],
                            'unit' => $list[$list_k]['unit'],
                            'unitconversion' => isset($list[$list_k]['unitconversion'])?$list[$list_k]['unitconversion']:'',
                            'stores' => $lists_v['name'],
                            'min_stock' => $lists_v['min_stock'],
                            'max_stock' => $lists_v['max_stock'],
                            'stock' => (float)$stock,
                        ];
                    }
                    unset($list[$list_k]);
                }else{
                    $list[$list_k]['stores'] = '全部仓库';
                }
            }
            foreach ($datas as $datas_k=>$datas_v){
                $list[] = $datas_v;
            }
            foreach ($list as $list_k=>$list_v){
                if($list_v['stock']>=$list_v['min_stock'] && $list_v['stock']<=$list_v['max_stock']){
                    unset($list[$list_k]);
                }
            }
            $lists = [];
            foreach ($list as $list_k=>$list_v){
                $lists[] = $list_v;
            }

            $result = array("count" => sizeof($lists), "list" => $lists);

            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    public function datainfo(){
        $data_type = $this->request->post('data_type');
        $data_type = $data_type==1?1:2;
        if($data_type==1){

        }

        //获取设置
        $set= \think\Db::name('kuyou_sets')->field('type,name,value,is_checked,set')->select();
        $setList = [];
        foreach ($set as $set_k=>$set_v){
            if(!empty($set_v['set'])){
                $set_v['set'] = unserialize($set_v['set']);
            }
            $setList[$set_v['name']] = $set_v;
        }
        //数量价格格式化
        $number_decimal = $setList['number_decimal']['value'];
        $price_decimal  = $setList['price_decimal']['value'];
        $today = $this->getTimestamp();
        $start_time = $today['start'];
        $end_time = $today['end'];

        $list = Db::name('kuyou_acceptance')
            ->where(['purchase_time'=>['between',[$start_time,$end_time]],'examine_status'=>'1'])
            ->select();
        $works = [];$work_hours = 0;
        foreach ($list as $list_k=>$list_v){
            $list[$list_k]['hours'] = 0;
            if ($list_v['code_type']==1){
                //加工单
                $production = Db::name('kuyou_production')->where(['purchase_code'=>$list_v['chase_code']])->find();
                if($production){
                    $production_id = Db::name('kuyou_plan')->where(['purchase_code'=>$production['plan_code']])->value('id');
                    $goods_list = Db::name('kuyou_plan_info')->where(['plan_id'=>$production_id])->field('goods_name,number,goods_code')->select();
                    $hours = 0;
                    foreach ($goods_list as $goods_list_k=>$goods_list_v){
                        $bom_id = Db::name('kuyou_bom')->where(['goods_code'=>$goods_list_v['goods_code']])->value('id');
                        $procedure = Db::name('kuyou_bom_procedure')->where(['bom_id'=>$bom_id])->select();
                        foreach ($procedure as $procedure_k=>$procedure_v){
                            $hours += $procedure_v['hours'];
                        }
                    }
                    $list[$list_k]['hours'] = round($hours,$number_decimal);
                }
            }else{
                //委外加工单
                $plan = Db::name('kuyou_outsourcing')->where(['purchase_code'=>$list_v['chase_code']])->find();
                $list[$list_k]['outsourcing_free'] = $plan['free_money'];
                if($plan){
                    $production_id = Db::name('kuyou_plan')->where(['purchase_code'=>$plan['plan_code']])->value('id');
                    $goods_list = Db::name('kuyou_plan_info')->where(['plan_id'=>$production_id])->field('goods_name,number,goods_code')->select();
                    $hours = 0;
                    foreach ($goods_list as $goods_list_k=>$goods_list_v){
                        $bom_id = Db::name('kuyou_bom')->where(['goods_code'=>$goods_list_v['goods_code']])->value('id');
                        $procedure = Db::name('kuyou_bom_procedure')->where(['bom_id'=>$bom_id])->select();
                        foreach ($procedure as $procedure_k=>$procedure_v){
                            $hours += $procedure_v['hours'];
                        }
                    }
                    $list[$list_k]['hours'] = round($hours,$number_decimal);
                }
            }
            if(!in_array($list_v['work_id'],$works)){
                $works[] = $list_v['work_id'];
            }
            $work_hours += $list[$list_k]['hours'];
        }

        $acceptance_id = Db::name('kuyou_acceptance')
            ->where(['purchase_time'=>['between',[$start_time,$end_time]],'examine_status'=>'1'])
            ->column('id');
        $count = Db::name('kuyou_acceptance_info')->where(['production_id'=>['in',$acceptance_id]])
            ->field('sum(need_number) as need_number,sum(number) as number,sum(bad_number) as bad_number')->find();
        $count['need_number'] = round($count['need_number'],$number_decimal);
        $count['number'] = round($count['number'],$number_decimal);
        $count['bad_number'] = round($count['bad_number'],$number_decimal);
        if($count['need_number']!=0){
            $count['number_pr'] = round($count['number']/$count['need_number']*100,$number_decimal);
            $count['bad_number_pr'] = round($count['bad_number']/$count['need_number']*100,$number_decimal);
        }
        $count['works'] = sizeof($works);$count['hours'] = $work_hours;

        $tj = [];
        $tj['count'] = $count;

        $jgd = [];
        $list = Db::name('kuyou_acceptance')
            ->where(['purchase_time'=>['between',[$start_time,$end_time]],'examine_status'=>'1','code_type'=>'1'])
            ->select();
        $works = [];$work_hours = 0;$need_number = 0;$number = 0;$bad_number = 0;
        foreach ($list as $list_k=>$list_v){
            $this_count = Db::name('kuyou_acceptance_info')->where(['production_id'=>$list_v['id']])
                ->field('sum(need_number) as need_number,sum(number) as number,sum(bad_number) as bad_number')->find();
            $need_number += $this_count['need_number'];$number += $this_count['number'];$bad_number += $this_count['bad_number'];
            $list[$list_k]['hours'] = 0;
            if ($list_v['code_type']==1){
                //加工单
                $production = Db::name('kuyou_production')->where(['purchase_code'=>$list_v['chase_code']])->find();
                if($production){
                    $production_id = Db::name('kuyou_plan')->where(['purchase_code'=>$production['plan_code']])->value('id');
                    $goods_list = Db::name('kuyou_plan_info')->where(['plan_id'=>$production_id])->field('goods_name,number,goods_code')->select();
                    $hours = 0;
                    foreach ($goods_list as $goods_list_k=>$goods_list_v){
                        $bom_id = Db::name('kuyou_bom')->where(['goods_code'=>$goods_list_v['goods_code']])->value('id');
                        $procedure = Db::name('kuyou_bom_procedure')->where(['bom_id'=>$bom_id])->select();
                        foreach ($procedure as $procedure_k=>$procedure_v){
                            $hours += $procedure_v['hours'];
                        }
                    }
                    $list[$list_k]['hours'] = round($hours,$number_decimal);
                }
            }else{
                //委外加工单
                $plan = Db::name('kuyou_outsourcing')->where(['purchase_code'=>$list_v['chase_code']])->find();
                $list[$list_k]['outsourcing_free'] = $plan['free_money'];
                if($plan){
                    $production_id = Db::name('kuyou_plan')->where(['purchase_code'=>$plan['plan_code']])->value('id');
                    $goods_list = Db::name('kuyou_plan_info')->where(['plan_id'=>$production_id])->field('goods_name,number,goods_code')->select();
                    $hours = 0;
                    foreach ($goods_list as $goods_list_k=>$goods_list_v){
                        $bom_id = Db::name('kuyou_bom')->where(['goods_code'=>$goods_list_v['goods_code']])->value('id');
                        $procedure = Db::name('kuyou_bom_procedure')->where(['bom_id'=>$bom_id])->select();
                        foreach ($procedure as $procedure_k=>$procedure_v){
                            $hours += $procedure_v['hours'];
                        }
                    }
                    $list[$list_k]['hours'] = round($hours,$number_decimal);
                }
            }
            if(!in_array($list_v['work_id'],$works)){
                $works[] = $list_v['work_id'];
            }
            $work_hours += $list[$list_k]['hours'];
        }
        $jgd['works'] = sizeof($works);$jgd['work_hours'] = $work_hours;$jgd['need_number'] = $need_number;$jgd['number'] = $number;$jgd['bad_number'] = $bad_number;
        $jgd['need_number'] = round($jgd['need_number'],$number_decimal);
        $jgd['number'] = round($jgd['number'],$number_decimal);
        $jgd['bad_number'] = round($jgd['bad_number'],$number_decimal);
        if($jgd['need_number']!=0){
            $jgd['number_pr'] = round($jgd['number']/$jgd['need_number']*100,$number_decimal);
            $jgd['bad_number_pr'] = round($jgd['bad_number']/$jgd['need_number']*100,$number_decimal);
        }
        $tj['jgd'] = $jgd;

        $wwjgd = [];
        $list = Db::name('kuyou_acceptance')
            ->where(['purchase_time'=>['between',[$start_time,$end_time]],'examine_status'=>'1','code_type'=>'2'])
            ->select();
        $works = [];$work_hours = 0;$need_number = 0;$number = 0;$bad_number = 0;
        foreach ($list as $list_k=>$list_v){
            $this_count = Db::name('kuyou_acceptance_info')->where(['production_id'=>$list_v['id']])
                ->field('sum(need_number) as need_number,sum(number) as number,sum(bad_number) as bad_number')->find();
            $need_number += $this_count['need_number'];$number += $this_count['number'];$bad_number += $this_count['bad_number'];
            $list[$list_k]['hours'] = 0;
            if ($list_v['code_type']==1){
                //加工单
                $production = Db::name('kuyou_production')->where(['purchase_code'=>$list_v['chase_code']])->find();
                if($production){
                    $production_id = Db::name('kuyou_plan')->where(['purchase_code'=>$production['plan_code']])->value('id');
                    $goods_list = Db::name('kuyou_plan_info')->where(['plan_id'=>$production_id])->field('goods_name,number,goods_code')->select();
                    $hours = 0;
                    foreach ($goods_list as $goods_list_k=>$goods_list_v){
                        $bom_id = Db::name('kuyou_bom')->where(['goods_code'=>$goods_list_v['goods_code']])->value('id');
                        $procedure = Db::name('kuyou_bom_procedure')->where(['bom_id'=>$bom_id])->select();
                        foreach ($procedure as $procedure_k=>$procedure_v){
                            $hours += $procedure_v['hours'];
                        }
                    }
                    $list[$list_k]['hours'] = round($hours,$number_decimal);
                }
            }else{
                //委外加工单
                $plan = Db::name('kuyou_outsourcing')->where(['purchase_code'=>$list_v['chase_code']])->find();
                $list[$list_k]['outsourcing_free'] = $plan['free_money'];
                if($plan){
                    $production_id = Db::name('kuyou_plan')->where(['purchase_code'=>$plan['plan_code']])->value('id');
                    $goods_list = Db::name('kuyou_plan_info')->where(['plan_id'=>$production_id])->field('goods_name,number,goods_code')->select();
                    $hours = 0;
                    foreach ($goods_list as $goods_list_k=>$goods_list_v){
                        $bom_id = Db::name('kuyou_bom')->where(['goods_code'=>$goods_list_v['goods_code']])->value('id');
                        $procedure = Db::name('kuyou_bom_procedure')->where(['bom_id'=>$bom_id])->select();
                        foreach ($procedure as $procedure_k=>$procedure_v){
                            $hours += $procedure_v['hours'];
                        }
                    }
                    $list[$list_k]['hours'] = round($hours,$number_decimal);
                }
            }
            if(!in_array($list_v['work_id'],$works)){
                $works[] = $list_v['work_id'];
            }
            $work_hours += $list[$list_k]['hours'];
        }
        $wwjgd['works'] = sizeof($works);$wwjgd['work_hours'] = $work_hours;$wwjgd['need_number'] = $need_number;$wwjgd['number'] = $number;$wwjgd['bad_number'] = $bad_number;
        $wwjgd['need_number'] = round($wwjgd['need_number'],$number_decimal);
        $wwjgd['number'] = round($wwjgd['number'],$number_decimal);
        $wwjgd['bad_number'] = round($wwjgd['bad_number'],$number_decimal);
        if($wwjgd['need_number']!=0){
            $wwjgd['number_pr'] = round($wwjgd['number']/$wwjgd['need_number']*100,$number_decimal);
            $wwjgd['bad_number_pr'] = round($wwjgd['bad_number']/$wwjgd['need_number']*100,$number_decimal);
        }
        $tj['wwjgd'] = $wwjgd;

        //折线统计图
        $time_arr = [];
        $time_start = strtotime(date('Y-m-d'))-86400*6;
        for ($a=0;$a<7;$a++){
            $row['start'] = $time_start+86400*$a;
            $row['end'] = $time_start+86400*($a+1)-1;
            $row['date'] = date('m-d',$row['start']);
            $time_arr[] = $row;
        }
        foreach ($time_arr as $time_arr_k=>$time_arr_v){
            $start_time = $time_arr_v['start'];$end_time = $time_arr_v['end'];
            $list = Db::name('kuyou_acceptance')
                ->where(['purchase_time'=>['between',[$start_time,$end_time]],'examine_status'=>'1'])
                ->select();
            $need_number = 0;$number = 0;$bad_number = 0;
            foreach ($list as $list_k=>$list_v){
                $this_count = Db::name('kuyou_acceptance_info')->where(['production_id'=>$list_v['id']])
                    ->field('sum(need_number) as need_number,sum(number) as number,sum(bad_number) as bad_number')->find();
                $need_number += $this_count['need_number'];$number += $this_count['number'];$bad_number += $this_count['bad_number'];
            }
            $time_arr[$time_arr_k]['need_number'] = $need_number;
            $time_arr[$time_arr_k]['number'] = $number;
            $time_arr[$time_arr_k]['bad_number'] = $bad_number;
        }
        $data = [];
        $xAxis = [];$series = [];
        $all_number = [];$number = [];$bad_number = [];
        foreach ($time_arr as $time_arr_k=>$time_arr_v){
            $xAxis[] = $time_arr_v['date'];
            $all_number[] = $time_arr_v['need_number'];
            $number[] = $time_arr_v['number'];
            $bad_number[] = $time_arr_v['bad_number'];
        }
        $series['all_number'] = $all_number;
        $series['number'] = $number;
        $series['bad_number'] = $bad_number;
        $data['xAxis'] = $xAxis;
        $data['series'] = $series;


        $list = Db::name('kuyou_acceptance')
            ->where(['examine_status'=>'1'])->order(['purchase_time'=>'DESC'])->limit(9)
            ->field('id,purchase_code')
            ->select();
        foreach ($list as $list_k=>$list_v){
            $this_count = Db::name('kuyou_acceptance_info')->where(['production_id'=>$list_v['id']])
                ->field('sum(need_number) as need_number,sum(number) as number,sum(bad_number) as bad_number')->find();
            $list_v['need_number'] = round($this_count['need_number'],$number_decimal);
            $list_v['number'] = round($this_count['number'],$number_decimal);
            $list_v['bad_number'] = round($this_count['bad_number'],$number_decimal);
            if($list_v['need_number']!=0){
                $list_v['number_pr'] = round($list_v['number']/$list_v['need_number']*100,$number_decimal);
                $list_v['bad_number_pr'] = round($list_v['bad_number']/$list_v['need_number']*100,$number_decimal);
            }
            $list[$list_k] = $list_v;
        }

        $acceptance_id = Db::name('kuyou_acceptance')
            ->where(['examine_status'=>'1'])
            ->column('id');
        $lists = Db::name('kuyou_acceptance_info')
            ->where(['production_id'=>['in',$acceptance_id]])
            ->group('goods_id')
            ->field('sum(need_number) as need_number,sum(number) as number,sum(bad_number) as bad_number,goods_name')
            ->order(['number'=>'DESC'])
            ->select();

        $result = array("countArr" => $tj,"dataArr" => $data,"list" => $list,"lists" => $lists);
        $this->success('查询成功',$result,200);
    }

    function getTimestamp($name = 'today', $param = '')
    {
        $nowtimr = strtotime(date('Y-m-d H') . ':' . (date('i') - date('i') % 10));
        switch ($name) {
            case 'now':
                $timeamp['start'] = $nowtimr - 600;
                $timeamp['end'] = $nowtimr;
                $timeamp['time'] = date('H:i', $timeamp['end']);
                break;
            case 'ten':
                $timeamp['start'] = $nowtimr - 1200;
                $timeamp['end'] = $nowtimr - 600;
                $timeamp['time'] = date('H:i', $timeamp['end']);
                break;
            case 'twenty':
                $timeamp['start'] = $nowtimr - 1800;
                $timeamp['end'] = $nowtimr - 1200;
                $timeamp['time'] = date('H:i', $timeamp['end']);
                break;
            case 'thirty':
                $timeamp['start'] = $nowtimr - 2400;
                $timeamp['end'] = $nowtimr - 1800;
                $timeamp['time'] = date('H:i', $timeamp['end']);
                break;
            case 'forty':
                $timeamp['start'] = $nowtimr - 3000;
                $timeamp['end'] = $nowtimr - 2400;
                $timeamp['time'] = date('H:i', $timeamp['end']);
                break;
            case 'fifty':
                $timeamp['start'] = $nowtimr - 3600;
                $timeamp['end'] = $nowtimr - 3000;
                $timeamp['time'] = date('H:i', $timeamp['end']);
                break;
            case 'today'://今天
                $timeamp['time'] = date('m-d');
                $timeamp['start'] = strtotime(date('Y-m-d'));
                $timeamp['end'] = strtotime(date('Y-m-d',strtotime('+1 day')))>time()?time():strtotime(date('Y-m-d',strtotime('+1 day')));
                break;
            case 'yesterday'://昨天
                $timeamp['time'] = date('m-d',strtotime('-1 day'));
                $timeamp['start'] = strtotime(date('Y-m-d',strtotime('-1 day')));
                $timeamp['end'] = strtotime(date('Y-m-d'));
                break;
            case 'beforyesterday'://前天
                $timeamp['time'] = date('m-d',strtotime('-2 day'));
                $timeamp['start'] = strtotime(date('Y-m-d',strtotime('-2 day')));
                $timeamp['end'] = strtotime(date('Y-m-d',strtotime('-1 day')));
                break;
            case 'threeday'://前3天
                $timeamp['time'] = date('m-d',strtotime('-3 day'));
                $timeamp['start'] = strtotime(date('Y-m-d',strtotime('-3 day')));
                $timeamp['end'] = strtotime(date('Y-m-d',strtotime('-2 day')));
                break;
            case 'fourday'://前4天
                $timeamp['time'] = date('m-d',strtotime('-4 day'));
                $timeamp['start'] = strtotime(date('Y-m-d',strtotime('-4 day')));
                $timeamp['end'] = strtotime(date('Y-m-d',strtotime('-3 day')));
                break;
            case 'fifday'://前5天
                $timeamp['time'] = date('m-d',strtotime('-5 day'));
                $timeamp['start'] = strtotime(date('Y-m-d',strtotime('-5 day')));
                $timeamp['end'] = strtotime(date('Y-m-d',strtotime('-4 day')));
                break;
            case 'sixday'://前6天
                $timeamp['time'] = date('m-d',strtotime('-6 day'));
                $timeamp['start'] = strtotime(date('Y-m-d',strtotime('-6 day')));
                $timeamp['end'] = strtotime(date('Y-m-d',strtotime('-5 day')));
                break;
            case 'beforweek'://本周
                $timeamp['start'] = strtotime(date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), date("d") - date("w") + 1, date("Y"))));
                $timeamp['end'] = strtotime(date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), date("d") - date("w") + 7, date("Y"))));
                break;
            case 'nowmonth'://本月
                $timeamp['start'] = strtotime(date('Y-m-01'));
                $timeamp['end'] = strtotime(date('Y-m-d',strtotime('+1 day')))>time()?time():strtotime(date('Y-m-d',strtotime('+1 day')));
                break;
            case 'permonth'://上月
                $timeamp['start'] = strtotime(date('Y-m-01', strtotime('-1 month')));
                $timeamp['end'] = strtotime(date('Y-m-01'));
                break;
            case 'preweek'://上周 注意我们是从周一开始算
                $timeamp['start'] = strtotime(date('Y-m-d', strtotime('-2 week Monday')));
                $timeamp['end'] = strtotime(date('Y-m-d', strtotime('-1 week Monday +1 day')));
                break;
            case 'nowweek'://本周
                $timeamp['start'] = strtotime(date('Y-m-d', strtotime('-1 week Monday')));
                $timeamp['end'] = strtotime(date('Y-m-d', strtotime('+1 day')));
                break;
            case 'preday'://30
                $timeamp['start'] = strtotime(date('Y-m-d'), strtotime($param . ' day'));
                $timeamp['end'] = strtotime(date('Y-m-d'));
                break;
            case 'nextday'://30
                $timeamp['start'] = strtotime(date('Y-m-d'));
                $timeamp['end'] = strtotime(date('Y-m-d'), strtotime($param . ' day'));
                break;
            case 'preyear'://去年
                $timeamp['start'] = strtotime(date('Y-01-01', strtotime('-1 year')));
                $timeamp['end'] = strtotime(date('Y-12-31', strtotime('-1 year')));
                break;
            case 'nowyear'://今年
                $timeamp['start'] = strtotime(date('Y-01-01'));
                $timeamp['end'] = strtotime(date('Y-m-d',strtotime('+1 day')))>time()?time():strtotime(date('Y-m-d',strtotime('+1 day')));
                break;
            case 'quarter'://季度
                $quarter = empty($param) ? ceil((date('n'))/3) : $param;//获取当前季度
                $timeamp['start'] = mktime(0, 0, 0,$quarter*3-2,1,date('Y'));
                $timeamp['end'] = mktime(0, 0, 0,$quarter*3+1,1,date('Y'))>time()?time():mktime(0, 0, 0,$quarter*3+1,1,date('Y'));
                break;
            case 'lastquarter'://季度
                $quarter = empty($param) ? ceil((date('n'))/3) : $param;//获取当前季度
                $quarter -= 1;
                if($quarter>0){
                    $timeamp['start'] = mktime(0, 0, 0,$quarter*3-2,1,date('Y'));
                    $timeamp['end'] = mktime(0, 0, 0,$quarter*3+1,1,date('Y'))>time()?time():mktime(0, 0, 0,$quarter*3+1,1,date('Y'));
                }else{
                    $quarter = 4;
                    $timeamp['start'] = mktime(0, 0, 0,$quarter*3-2,1,(date('Y')-1));
                    $timeamp['end'] = mktime(0, 0, 0,$quarter*3+1,1,(date('Y')-1))>time()?time():mktime(0, 0, 0,$quarter*3+1,1,(date('Y')-1));
                }
                break;
            case 'lastyeayquarter'://去年同季度
                $quarter = empty($param) ? ceil((date('n'))/3) : $param;//获取当前季度
                $timeamp['start'] = mktime(0, 0, 0,$quarter*3-2,1,(date('Y')-1));
                $timeamp['end'] = mktime(0, 0, 0,$quarter*3+1,1,(date('Y')-1))>time()?time():mktime(0, 0, 0,$quarter*3+1,1,(date('Y')-1));
                break;
            default:
                $timeamp['start'] = strtotime(date('Y-m-d'));
                $timeamp['end'] = strtotime(date('Y-m-d', strtotime('+1 day')));
                break;
        }
        if($timeamp['end']>time()){
            $timeamp['end'] = time();
        }
        return $timeamp;
    }
}
